package com.marion.mzuul.common;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class RequestInterceptor {

    @Pointcut(value = "@annotation(RequestLog)")
    public void requestLog() {

    }

    @Around(value = "requestLog()")
    public Object requestAround(ProceedingJoinPoint point) {

        long st = System.currentTimeMillis();
        Object ret = null;

        MethodSignature signature = (MethodSignature) point.getSignature();

        try {
           ret = point.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

        long et = System.currentTimeMillis();
        log.info("[{}] exe={}m", signature.getMethod(), et - st);

        return ret;
    }
}
